package com.example.flashsales.controller;


import com.example.flashsales.entity.Result;
import com.example.flashsales.entity.vo.StoreCostRecordVo;
import com.example.flashsales.service.impl.StoreCostRecordServiceImpl;
import com.example.flashsales.service.impl.StoreServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/store/costRecord")
public class StoreCostRecordController {

    @Autowired
    StoreServiceImpl storeService;

    @Autowired
    StoreCostRecordServiceImpl storeCostRecordService;

    /**
     * 店家查看所有的流水纪录
     * @param httpServletRequest
     * @param num
     * @param page
     * @return
     */
    @ResponseBody
    @GetMapping("/viewAllStoreCostRecord")
    public Result viewAllStoreCostRecord(HttpServletRequest httpServletRequest, Integer num, Integer page){
        if (num == null || page == null){
            return Result.error("数据不能为空");

        }
        if (page <= 0){
            return Result.error("页数不能小于等于0");
        }
        long id = Integer.parseInt(httpServletRequest.getAttribute("id") + "");
        final List<StoreCostRecordVo> storeCostRecordVos = storeService.viewAllStoreCostRecord(id, page, num);
        Map<String,Object> map = new HashMap<>();
        map.put("total",storeCostRecordService.viewCount(id));
        map.put("data",storeCostRecordVos);
        return Result.ok("查看成功").setData(map);
    }


    /**
     * 按时间和类型查看流水纪录
     * @param httpServletRequest
     * @param qDate
     * @param hDate
     * @param page
     * @param num
     * @param state -1 全部，0 提现，1拉人，2取货
     * @return
     */
    @ResponseBody
    @GetMapping("/viewStoreRecordByTimeAndState")
    public Result viewStoreRecordByTimeAndState(HttpServletRequest httpServletRequest, Date qDate, Date hDate, Integer page, Integer num, Integer state){
        if (qDate == null || hDate == null || num == null || page == null){
            return Result.error("数据不能为空");

        }
        if (page <= 0){
            return Result.error("页数不能小于等于0");
        }
        long id = Integer.parseInt(httpServletRequest.getAttribute("id") + "");
        final List<StoreCostRecordVo> storeCostRecordVos = storeService.viewStoreRecordByTimeAndState(id, qDate, hDate, page, num, state);
        Map<String,Object> map = new HashMap<>();
        map.put("total",storeCostRecordService.viewCountByTime(id,qDate,hDate,state));
        map.put("data",storeCostRecordVos);
        return Result.ok("查看成功").setData(map);
    }


}
